package com.fernfx.xingtan.contacts.presenter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.contacts.NewFriendsAdapter;
import com.fernfx.xingtan.contacts.contract.NewFriendsContract;
import com.fernfx.xingtan.contacts.entity.NewFriendsEntity;
import com.fernfx.xingtan.contacts.model.NewFriendsModel;
import com.fernfx.xingtan.contacts.ui.NewFriendsActivity;
import com.fernfx.xingtan.main.entity.ContactsInfoEntity;
import com.fernfx.xingtan.main.ui.ContactsInfoFragment;
import com.fernfx.xingtan.utils.CollectionUtil;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/8/7.
 */

public class NewFriendsPresenter<P extends NewFriendsContract.View> implements NewFriendsContract.Presenter {
    private P P;
    private NewFriendsContract.Model model;
    //adapter
    private NewFriendsAdapter adapter = null;
    public List<NewFriendsEntity.ObjBean.UserRelationVOListBean> userRelationVOList;
    private NewFriendsActivity newFriendsActivity;
    //备份list
    public final List<NewFriendsEntity.ObjBean.UserRelationVOListBean> cacheUserRelationVOList = new ArrayList<>(5);

    private ArrayList<ContactsInfoEntity.ObjBean.RecordsBean> contactsInfoList;

    @Override
    public void init(BaseView P) {
        this.P = (P)P;
        this.model = new NewFriendsModel();
        //init adpater
        userRelationVOList = new ArrayList<>();
        adapter = new NewFriendsAdapter(P.getActivity(),userRelationVOList);
        //adpater set presenter
        adapter.setNewFriendsPresenter(this);
        newFriendsActivity = (NewFriendsActivity)P.getActivity();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {

    }

    @Override
    public void detachView() {
        if (null != model) {
            model = null;
        }
        if (CollectionUtil.isEmpty(userRelationVOList)) {
            userRelationVOList.clear();
        }
    }

    @Override
    public void getRequestFriends() {
        P.getActivity().showLoading();
        model.getRequestFriends(new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                P.getActivity().hideLoading();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(),500);
                    return;
                }

                NewFriendsEntity newFriendsEntity = FastJsonUtil.fromBean(message, NewFriendsEntity.class);
                if (OtherUtil.checkRequestStatus(newFriendsEntity.getSuccess(),newFriendsEntity.getStatus())) {
                    if (!CollectionUtil.isEmpty(newFriendsEntity.getObj().getUserRelationVOList())){
                        userRelationVOList.addAll(newFriendsEntity.getObj().getUserRelationVOList());
                        cacheUserRelationVOList.addAll(userRelationVOList);
                    }
                    //Listview
                    ListView firendsLiv = newFriendsActivity.getFriendsInfoLiv();
                    //set adapter
                    firendsLiv.setAdapter(adapter);
                }else {
                    ToastUtil.showCentertoast(newFriendsEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage,errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                P.getActivity().hideLoading();
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public List<NewFriendsEntity.ObjBean.UserRelationVOListBean> getUserRelationVOList() {
        return userRelationVOList;
    }

    @Override
    public NewFriendsAdapter getNewFriendsAdapter() {
        return adapter;
    }

    @Override
    public List<NewFriendsEntity.ObjBean.UserRelationVOListBean> getCacheUserRelationVOList() {
        return cacheUserRelationVOList;
    }

    @Override
    public void operationAddFriends(final NewFriendsAdapter newFriendsAdapter, final NewFriendsEntity.ObjBean.UserRelationVOListBean userRelationVOListBean,
                                    Map<String, Object> requestArgsMap,final NewFriendsAdapter.StatusEntity statusEntity) {
        model.operationAddFriends(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(message,500);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
                if (OtherUtil.checkRequestStatus(baseEntity.getSuccess(),baseEntity.getStatus())) {
                    //修改本页面状态并提醒
                    newFriendsAdapter.addFriendsSucess(userRelationVOListBean,statusEntity);
                    ToastUtil.showCentertoast("已添加");
                }else{
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage,errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public void setContactsInfoList(ArrayList<ContactsInfoEntity.ObjBean.RecordsBean> contactsInfoList) {
        this.contactsInfoList = contactsInfoList;
    }
}
